﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS
{
    #region Using Statements

    using System.Text;
    using System.Collections.Generic;

    #endregion

    /// <summary>
    /// The CSSValueList interface provides the abstraction of an ordered 
    /// collection of CSS values.
    /// 
    /// Some properties allow an empty list into their syntax. In that case, 
    /// these properties take the none identifier. So, an empty list means
    /// that the property has the value none.
    /// 
    /// The items in the CSSValueList are accessible via an integral index,
    /// starting from 0.
    /// </summary>
    public class CSSValueList : CSSValue
    {
        #region Constructors

        internal CSSValueList()
        {
            Items = new List<CSSValue>();
            cssValueType = CSS_VALUE_LIST;
        }

        #endregion

        #region Attributes

        public List<CSSValue> Items { get; set; }

        #endregion

        #region Methods

        internal void AddValue(CSSValue value)
        {
            if (!Items.Contains(value))
                Items.Add(value);
        }

        #endregion

        #region DOM Level 2 CSS Compliance

        public override string cssText
        {
            get
            {
                StringBuilder sb = new StringBuilder();

                foreach (var item in Items)
                {
                    if (sb.Length > 0)
                        sb.Append(", ");

                    if (item != null && item.cssText != null)
                        sb.Append(item.cssText);
                    else
                        sb.Append("");
                }

                return sb.ToString();
            }
            set
            {
                base.cssText = value;
            }
        }

        /// <summary>
        /// Used to retrieve a CSSValue by ordinal index. The order in this 
        /// collection represents the order of the values in the CSS style
        /// property. If index is greater than or equal to the number of 
        /// values in the list, this returns null. 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public CSSValue item(int index)
        {
            return (index > -1 && index < Items.Count ? Items[index] : null);
        }

        /// <summary>
        /// The number of CSSValues in the list. The range of valid values 
        /// of the indices is 0 to length-1 inclusive.
        /// </summary>
        public int length
        {
            get
            {
                return Items.Count;
            }
        }

        #endregion
    }
}
